/**
 * @author ens12ilt - ens12ple
 */

package parser;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import model.Image;

public final class AnswerParser {
	
	private static final String REGEX_ANSWER_LINE = "^Image[0-9]+ [0-9]";

	/**
	 * 
	 * @param fileName facit file name
	 * @param images images
	 * @throws IOException
	 */
	public static void parseAndSetAnswerToImages(String fileName, ArrayList<Image> images) throws IOException{
		FileReader in = new FileReader(fileName);
		BufferedReader buffer = new BufferedReader(in);
		String line;
		while((line = buffer.readLine()) != null){
			if(line.matches(REGEX_ANSWER_LINE)){
				String imageName = line.split(" ")[0];
				int type = Integer.parseInt(line.split(" ")[1]);
				int i = 0;
				while(i < images.size() && !images.get(i).getName().equals(imageName)){
					i++;
				}
				images.get(i).setType(type);
			}
		}
	}
}
